<template>
  <div class="app-container">
    <el-row :gutter="10" style="position: relative; margin-bottom: 5px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:materiel:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#productBomInfo', '物料品质标准')"
          v-hasPermi="['project:materiel:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      id="productBomInfo"
      v-loading="loading"
      border
      highlight-current-row
      :height="tableH"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      :data="materielList"
      @row-click="getChildList"
    >
      <el-table-column label="#" fixed="left" type="index" align="center" />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="itmWldm"
      />
      <el-table-column
        label="物料品名"
        :show-overflow-tooltip="true"
        width="110"
        prop="itmWlpm"
      />
      <el-table-column
        label="物料规格"
        :show-overflow-tooltip="true"
        width="330"
        prop="itmWlgg"
      />
      <el-table-column
        label="材质"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmCzdm"
      />
      <el-table-column
        label="颜色"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmYsdm"
      />
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="itmCustwldm"
      />
      <el-table-column
        label="单位净重(kg)"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmJz"
      />
      <el-table-column
        label="单位毛重(kg)"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="itmMz"
      />
      <el-table-column
        label="巡检模板"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="itmIxName"
      />
      <el-table-column
        label="检测尺寸1"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc1"
      />
      <el-table-column
        label="检测尺寸1上限值"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc1S"
      />
      <el-table-column
        label="检测尺寸1下限值"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc1X"
      />
      <el-table-column
        label="检测尺寸2"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc2"
      />
      <el-table-column
        label="检测尺寸2上限制"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc2S"
      />
      <el-table-column
        label="检测尺寸2下限制"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc2X"
      />
      <el-table-column
        label="检测尺寸3"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc3"
      />
      <el-table-column
        label="检测尺寸3上限值"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc3S"
      />
      <el-table-column
        label="检测尺寸3下限值"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmJccc3X"
      />
      <el-table-column
        label="△标准颜色"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="itmStandardColor"
      />
      <el-table-column
        label="更改人员"
        width="80"
        align="center"
        prop="updateBy"
      />
      <el-table-column
        label="更改日期"
        align="center"
        prop="updateTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框 -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label-width="125px" label="仓库代码" prop="itmCkdm">
            <el-select
              v-model="queryParams.itmCkdm"
              placeholder="请输入仓库代码"
            >
              <el-option
                v-for="dict in storageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="totalOrder">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.totalOrder"
              placeholder="请输入物料代码或品名规格或客户编码"
              clearable
              style="width: 400px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" label="物料状态" prop="itmType">
            <el-radio-group v-model="queryParams.itmType">
              <el-radio label="W">生产物料</el-radio>
              <el-radio label="Q">非生产物料</el-radio>
              <el-radio lable="">全部</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="display: flex; margin-top: 20px; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 修改生产物料信息对话框 -->
    <el-dialog
      :title="title"
      v-model="openStr"
      :close-on-click-modal="false"
      draggable
      width="880px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="物料代码" prop="itmWldm">
            <el-input
              v-model="form.itmWldm"
              disabled
              placeholder="请输入物料代码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="right">
          <el-form-item label="物品品名" prop="itmWlpm">
            <el-input
              v-model="form.itmWlpm"
              disabled
              placeholder="请输入物品品名"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材质" prop="itmCzdm">
            <el-input
              v-model="form.itmCzdm"
              disabled
              placeholder="请输入材质"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="right">
          <el-form-item label="颜色" prop="itmYsdm">
            <el-input
              v-model="form.itmYsdm"
              disabled
              placeholder="请输入颜色"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="尺寸描述" prop="itmCcms">
            <el-input
              v-model="form.itmCcms"
              disabled
              placeholder="请输入尺寸描述"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="单位净重" prop="itmJz">
            <el-input v-model="form.itmJz" disabled type="number" />
          </el-form-item>
        </el-col>
        <el-col :span="1" class="unit-parent">
          <span class="unit">kg</span>
        </el-col>
        <el-col :span="24" class="right">
          <el-form-item label="客户编码" prop="itmCustwldm">
            <el-input
              v-model="form.itmCustwldm"
              disabled
              placeholder="请输入客户编码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸1" prop="itmJccc1">
            <el-input v-model="form.itmJccc1" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸1上限值" prop="itmJccc1S">
            <el-input v-model="form.itmJccc1S" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸1下限值" prop="itmJccc1X">
            <el-input v-model="form.itmJccc1X" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸2" prop="itmJccc2">
            <el-input v-model="form.itmJccc2" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸2上限值" prop="itmJccc2S">
            <el-input v-model="form.itmJccc2S" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸2下限值" prop="itmJccc2X">
            <el-input v-model="form.itmJccc2X" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸3" prop="itmJccc3">
            <el-input v-model="form.itmJccc3" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸3上限值" prop="itmJccc3S">
            <el-input v-model="form.itmJccc3S" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检测尺寸3下限值" prop="itmJccc3X">
            <el-input v-model="form.itmJccc3X" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="right">
          <el-form-item label="△标准颜色" prop="itmStandardColor">
            <el-input
              v-model="form.itmStandardColor"
              placeholder="请输入标准颜色"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="巡检模板" prop="itmIxType">
            <el-select
              :value="typeIx"
              placeholder="请选择巡检模板"
              value-key="dictValue"
              @change="
                (e) => {
                  form.itmIxType = e.dictValue;
                  form.itmIxName = e.dictLabel;
                }
              "
            >
              <el-option
                v-for="dict in inspectionTableList"
                :key="dict.dictValue"
                :value="dict"
                :label="dict.dictLabel"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelStr">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  generateItemCode,
  getMateriel,
  listMaterielQuality,
  updateMateriel,
} from "@/api/project/materiel";
import { getConfigKey } from "@/api/system/config";
import { getAllJzm } from "@/api/project/jzmMstr";
import { getAll } from "@/api/repertory/Storage";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "ProductBomInfo",
  data() {
    return {
      //物料Id
      itmMstr: null,
      //客户ID
      cusId: null,
      //客户分类
      cus: [],
      //大分类信息
      bigLbm: [],
      //中分类
      middleLbm: [],
      //小分类
      smallLbm: [],
      //仓位信息
      storageList: [],
      //当前物料代码
      itmData: null,
      //添加厂商对话框
      addCsmView: false,
      //所有厂商
      allCsmData: [],
      //物料厂商列表
      csmList: [],
      //仓库信息
      storageLists: [],
      //机种信息
      jzmList: [],
      //分类Form
      lbmForm: {},
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight - 300,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      timer: null,
      itmImg: null,
      //选中的物料名称
      itmWldms: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产物料信息表格数据
      materielList: [],
      // 弹出层标题
      title: "",
      // 是否弹出新增对话框
      open: false,
      openSearch: false,
      //是否弹出修改对话框
      openStr: false,
      //上传文件对话框
      openFile: false,
      // 巡检表字典
      inspectionTableList: [],
      //有效标识
      csmWldmFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        totalOrder: null,
        itmWldm: null,
        itmPmgg: null,
        itmCustwldm: null,
        itmCkdm: null,
        createBy: null,
        createTimes: null,
        updateBy: null,
        updateTime: null,
        beginDate: null,
        endDate: null,
        itmType: "W",
      },
      // 表单参数
      form: { itmIxType: null },
      csmForm: {},
      // 表单校验
      rules: {
        itmWldm: [
          { required: true, message: "物品代码不能为空", trigger: "blur" },
        ],
        itmWlpm: [
          { required: true, message: "物品品名不能为空", trigger: "blur" },
        ],
        itmDw: [{ required: true, message: "单位不能为空", trigger: "change" }],
        itmMinbz: [
          { required: true, message: "最小包装不能为空", trigger: "blur" },
        ],
        itmCwdm: [
          { required: true, message: "仓位代码不能为空", trigger: "blur" },
        ],
        itmCkdm: [
          { required: true, message: "仓库代码不能为空", trigger: "blur" },
        ],
        cusId: [{ required: true, message: "客户不能为空", trigger: "change" }],
        bigLbmId: [
          { required: true, message: "大分类不能为空", trigger: "change" },
        ],
        middleLbmId: [
          { required: true, message: "中分类不能为空", trigger: "change" },
        ],
      },
      csmFormRules: {
        itcslCsdm: [
          { required: true, message: "厂商代码不能为空", trigger: "blur" },
        ],
        supplyQuota: [
          { required: true, message: "供货配额不能为空", trigger: "blur" },
        ],
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  computed: {
    typeIx() {
      return { dictLabel: this.form.itmIxName, dictValue: this.form.itmIxType };
    },
  },
  created() {
    this.getList();
    // 巡检表字典
    this.getDicts("Inspection_table").then((response) => {
      this.inspectionTableList = response.data;
    });
    getAll().then((response) => {
      this.storageLists = response.data;
    });
  },
  methods: {
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产物料信息";
      this.openSearch = true;
    },
    /** 查询生产物料信息列表 */
    getList() {
      this.loading = true;
      listMaterielQuality(this.queryParams).then((response) => {
        this.materielList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    cancelStr() {
      this.openStr = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itmId: null,
        itmWldm: null,
        itmPrice: null,
        itmWlpm: null,
        itmWlgg: null,
        itmPmgg: null,
        itmCzdm: null,
        itmYsdm: null,
        itmQtinf: null,
        itmMz: null,
        itmJz: 0,
        itmSkzl: 0,
        itmDw: null,
        itmCustwldm: null,
        itmTzbh: null,
        itmCgsx: null,
        itmMinbz: 0,
        itmCwdm: null,
        itmCkdm: null,
        itmKwdm: null,
        itmJzdm: null,
        itmCcms: null,
        itmZtbz: null,
        itmEdzq: 0,
        itmTedzp: null,
        itmShry: null,
        itmShrq: null,
        itmPzry: null,
        itmPzrq: null,
        itmJccc1: null,
        itmJccc1S: null,
        itmJccc1X: null,
        itmJccc2: null,
        itmJccc2S: null,
        itmJccc2X: null,
        itmJccc3: null,
        itmJccc3S: null,
        itmJccc3X: null,
        itmJczl: null,
        itmCj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        cusId: null,
        bigLbmId: null,
        middleLbmId: null,
        smallLbmId: null,
        cusCode: null,
        bigLbmCode: null,
        middleLbmCode: null,
        smallLbmCode: null,
        itmStandardColor: null,
        itmIxType: null,
        itmIxName: null,
      };
      this.csmForm = {
        itcslId: null,
        itcslCsdm: null,
        itcslCsmc: null,
        supplyQuota: null,
        itcslWldm: null,
        itmPmgg: null,
        remark: null,
        itcslFlag: "0",
      };

      this.resetForm("csmForm");
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.createTimes != null) {
        this.queryParams.beginDate = this.queryParams.createTimes[0];
        this.queryParams.endDate = this.queryParams.createTimes[1];
      }
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    generate(data) {
      this.form.itmWldm = null;
      //TODO 直接生成物料代码
      getConfigKey("sys.materrial.prefix").then((response) => {
        //开始构建参数
        let wldm =
          response.msg +
          "." +
          this.form.cusCode +
          "." +
          this.form.bigLbmCode +
          "." +
          this.middleLbmCode +
          "." +
          data.label +
          ".";
        //向后端发送请求
        generateItemCode(wldm).then((response) => {
          this.form.itmWldm = response.msg;
        });
      });
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      const itmId = this.itmData.itmId;
      getMateriel(itmId).then((response) => {
        getAllJzm().then((res) => {
          this.jzmList = res.data;
        });
        this.form = response.data;
        this.openStr = true;
        this.title = "修改生产物料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.itmDw == "unit_3" && this.form.itmJz == 0) {
        this.msgError("净重不能为0");
        return 0;
      }
      this.$refs["form"].validate((valid) => {
        updateMateriel(this.form).then((response) => {
          this.msgSuccess("修改成功");
          this.openStr = false;
          this.getList();
        });
      });
    },
    /** 导出按钮操作 */
    handleExport(id, title) {
      exportTable(id, title);
    },
    getChildList(row) {
      this.itmData = row;
      this.single = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.unit-parent {
  height: 34px;
}

.unit {
  margin-left: 10px;
  height: 34px;
  line-height: 34px;
}

.unit-button {
  margin-left: 10px;
  height: 38px;
  line-height: 38px;
}

.el-table .cell {
  white-space: nowrap;
}
</style>
